﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using Aden.Model.Net;
using Aden.Model.WeChat;
using Aden.BLL.WeChat;


namespace AdenWebAPI.Controllers
{
    public class WechatController : BaseController
    {
        [HttpPost]
        public Response GetCodeUrl(string token, [FromBody]WeChatInfo param)
        {
            Response response = new Response();

            string[] strAry = token.Split(',');
            string tmpToken = strAry[0];
            string appName = strAry[1];

            if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token))
            {
                response.code = "404";
                response.message = "Invild token";
            }
            else
            {
                try
                {
                    var data = WechatHelper.GetCodeUrl(appName,param.redirectUrl);
                    if (data == null)
                        throw new Exception("No data");

                    response.code = "200";
                    response.content = data;

                }
                catch (Exception e)
                {
                    response.code = "500";
                    response.message = e.Message;
                }
            }

            return response;
        }

        [HttpPost]
        public Response GetUserInfo(string token, [FromBody]WeChatInfo param)
        {
            Response response = new Response();

            string[] strAry = token.Split(',');
            string tmpToken = strAry[0];
            string appName = strAry[1];

            if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token))
            {
                response.code = "404";
                response.message = "Invild token";
            }
            else
            {
                try
                {
                    var data = WechatHelper.GetUserInfo(param, appName);
                    if (data == null)
                        throw new Exception("No data");

                    response.code = "200";
                    response.content = data;

                } catch(Exception e)
                {
                    response.code = "500";
                    response.message = e.Message;
                }
            }

            return response;
        }

        [HttpGet]
        public Response GetUser(string token)
        {
            Response response = new Response();

            string[] strAry = token.Split(',');
            string tmpToken = strAry[0];
            string appName = strAry[1];

            if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token))
            {
                response.code = "404";
                response.message = "Invild token";
            }
            else
            {
                try
                {
                    var data = WechatHelper.GetCode(appName);
                    if (data == null)
                        throw new Exception("No data");

                    response.code = "200";
                    response.content = data;

                }
                catch (Exception e)
                {
                    response.code = "500";
                    response.message = e.Message;
                }
            }

            return response;
        }

        [HttpGet]
        public Response GetJSApiTicket(string token)
        {
            Response response = new Response();

            string[] strAry = token.Split(',');
            string tmpToken = strAry[0];
            string appName = strAry[1];

            if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token))
            {
                response.code = "404";
                response.message = "Invild token";
            }
            else
            {
                try
                {
                    var data = WechatHelper.GetJSApiTicket(appName);
                    if (data == null)
                        throw new Exception("No data");

                    response.code = "200";
                    response.content = data;

                }
                catch (Exception e)
                {
                    response.code = "500";
                    response.message = e.Message;
                }
            }

            return response;
        }
    }
}
